# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui

#_______________________________________________________________________________
class CollectionTreeView(QtGui.QTreeView):
    songs_added = QtCore.pyqtSignal(list, bool)
    
    def __init__(self, parent):
        QtGui.QTreeView.__init__(self, parent)
        self.set_context_menu(self)
        self.setEnabled(False)
        self.setAcceptDrops(True)
        
    def set_context_menu(self, parent):
        self.context_menu = QtGui.QMenu("Context Menu", parent)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/images/add.png"), 
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.add_action = self.context_menu.addAction(icon, "Add To Playlist")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/images/replace.png"), 
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.replace_action = self.context_menu.addAction(icon, "Replace")
        self.add_action.triggered.connect(self.on_add_action_triggered)
        self.replace_action.triggered.connect(self.on_replace_action_triggered)        
        
    def setModel(self, model):
        self.model = model
        self.setEnabled(True)
        QtGui.QTreeView.setModel(self, model)
        self.sortByColumn(0, QtCore.Qt.AscendingOrder)
    
    def on_add_action_triggered(self):
        indexes = self.selectedIndexes()
        if indexes:
            self.model.add_songs_to_playlist(indexes)
            self.clearSelection()
    
    def on_replace_action_triggered(self):
        indexes = self.selectedIndexes()
        if indexes:
            self.model.add_songs_to_playlist(indexes, True)
            self.clearSelection()
    
    def mousePressEvent(self, me):
        QtGui.QTreeView.mousePressEvent(self, me)
        if (me.button() == QtCore.Qt.RightButton and 
            me.modifiers() == QtCore.Qt.NoModifier):
            self.context_menu.popup(me.globalPos())
